/******************************************************************************
 *
 * ngcap - network sniffer using raw sockets
 *
 * Author:        cr
 * E-mail:        cr.at.netgarage.org
 * WWW:           http://www.netgarage.org
 * Copyright (C): 2009 by cr
 *
 * This file is part of ngcap.
 *
 * ngcap is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * ngcap is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with ngcap.  If not, see <http://www.gnu.org/licenses/>.
 *
 *****************************************************************************/

#ifndef _PACKETS_H_
#define _PACKETS_H_
//#include <sys/socket.h>
//#include <netinet/in.h>
#include <arpa/inet.h>
#include <string>
#define ETHADDRLEN	6
#define ETHERTYPE_IP            0x0800          // ip protocol
#define ETHERTYPE_ARP           0x0806          // arp protocol
#define ETHERTYPE_REVARP        0x8035          // rarp protocol
#define ETHERTYPE_IPV6          0x86DD          //ipv6 protocol

const int MTU=1518;
const int ethernet_size=14;
const int icmp_size=4;
const int udp_size=4;

//struct for information about a captured packet

typedef struct
{
	in_addr src_addr;
	in_addr dst_addr;
	unsigned short int sport;
	unsigned short int dport;
	std::string protocol;
	std::string data;
	int packetcount;
	std::string packet_infos;
	std::string eth_header;
} captured_packet;

// ethernet header

typedef struct
{
	unsigned char   h_dest[ETHADDRLEN];       //destination ether address
	unsigned char   h_source[ETHADDRLEN];     //source ether address
	unsigned short  h_proto;               	  //ether protocol
} eth_hdr;

// arp definitions and header

#define ARPOP_REQUEST   1               // ARP request
#define ARPOP_REPLY     2               // ARP reply
#define ARPOP_RREQUEST  3               // RARP request
#define ARPOP_RREPLY    4               // RARP reply
#define ARPOP_InREQUEST 8               // InARP request
#define ARPOP_InREPLY   9               // InARP reply
#define ARPOP_NAK       10              // ARP NAK

struct arphdr
  {
    unsigned short int ar_hrd;          // Format of hardware address
    unsigned short int ar_pro;          // Format of protocol address
    unsigned char ar_hln;               // Length of hardware address
    unsigned char ar_pln;               // Length of protocol address
    unsigned short int ar_op;           // ARP opcode (command)
  };

typedef struct
{
        struct  arphdr ea_hdr;               // fixed-size header
        unsigned char arp_sha[ETHADDRLEN];   // sender hardware address
        unsigned char arp_spa[4];            // sender protocol address
        unsigned char arp_tha[ETHADDRLEN];   // target hardware address
        unsigned char arp_tpa[4];            // target protocol address
} arp_packet;

// ip header

typedef struct
{
	unsigned char  ihl:4;         // headerlenght
	unsigned char  version:4;     // ip version
	unsigned char  tos;           // type of service
	unsigned short tot_len;       // packet lenght
	unsigned short id;            // unique identifier
	unsigned short frag_off;      // flags
	unsigned char  ttl;           // time to live
	unsigned char  protocol;      // protocol
	unsigned short check;         // ip checksum
	unsigned int   saddr;         // source ip address
	unsigned int   daddr;         // destination ip address
} ip_hdr;

// tcp header

typedef struct
  {
    unsigned short source;          // sourceport
    unsigned short dest;            // destination port
    unsigned int seq;               // sequence number
    unsigned int ack_seq;           // acknowledgement number
    unsigned short res1:4;          // unused
    unsigned short doff:4;          // data offset
    unsigned short fin:1;           // bitfield flags definition
    unsigned short syn:1;
    unsigned short rst:1;
    unsigned short psh:1;
    unsigned short ack:1;
    unsigned short urg:1;
    unsigned short res2:2;
    unsigned short window;          // windowsize
    unsigned short check;           // checksum
    unsigned short urg_ptr;         // urgent pointer
} tcp_hdr;

// udp header

typedef struct
{
  unsigned short source;        // sourceport
  unsigned short dest;          // destination port
  unsigned short len;           // packet length
  unsigned short check;         // checksum
} udp_hdr;

// icmp definitions and header

#define ICMP_ECHOREPLY          0       // echo reply
#define ICMP_DEST_UNREACH       3       // destination unreachable
#define ICMP_SOURCE_QUENCH      4       // source quench
#define ICMP_REDIRECT           5       // redirect
#define ICMP_ECHO               8       // echo request
#define ICMP_TIME_EXCEEDED      11      // time exceeded
#define ICMP_PARAMETERPROB      12      // parameter problem
#define ICMP_TIMESTAMP          13      // timestamp request
#define ICMP_TIMESTAMPREPLY     14      // timestamp reply
#define ICMP_INFO_REQUEST       15      // information request
#define ICMP_INFO_REPLY         16      // information reply
#define ICMP_ADDRESS            17      // address mask request
#define ICMP_ADDRESSREPLY       18      // address mask reply

typedef struct
{
  unsigned char type;           // message type
  unsigned char code;           // type sub code
  unsigned short checksum;      // checksum
  union
  {
    struct
    {
      unsigned short id;
      unsigned short sequence;
    } echo;                     // echo datagram
    unsigned int   gateway;         // gateway address
    struct
    {
      unsigned short unused;
      unsigned short mtu;
    } frag;                     // path mtu discovery
  } un;
} icmp_hdr;

#endif
